package io.renren.modules.oa.controller;

import io.renren.common.utils.R;
import io.renren.modules.oa.entity.DeptEntity;
import io.renren.modules.oa.service.DeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 部门表
 *
 * @author liufh
 * @email sunlightcs@gmail.com
 * @date 2021-05-20 14:45:22
 */
@RestController
@RequestMapping("oa/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("oa:dept:list")
    public List<DeptEntity> list(@RequestParam Map<String, Object> params){
//        List<DeptEntity> deptList = deptService.list();
        List<DeptEntity> deptList = deptService.queryByCondition((String)params.get("deptName"));

        return deptList;

//        PageUtils page = deptService.queryPage(params);
//        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    @RequiresPermissions("oa:dept:info")
    public R info(@PathVariable("deptId") Long deptId){
		DeptEntity dept = deptService.getById(deptId);

        return R.ok().put("dept", dept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("oa:dept:save")
    public R save(@RequestBody DeptEntity dept){
        dept.setCreateTime(new Date());
		deptService.save(dept);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("oa:dept:update")
    public R update(@RequestBody DeptEntity dept){
        dept.setUpdateTime(new Date());
		deptService.updateById(dept);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("oa:dept:delete")
    public R delete(@RequestBody Long deptId){
        List<DeptEntity> deptList = deptService.getByParentId(deptId);
        if (deptList == null) {
            deptService.removeById(deptId);
            return R.ok();
        } else {
            return R.error(999, "请先删除子部门");
        }
    }

}
